<?php
/**
  * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说 图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9 0009
 * Time: 11:01
 */
defined("IN_IA")or exit("Access denied");
global $_GPC,$_W;
$uniacid=$_GPC['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"order_list";
$uid=$_GPC['uid'];

if($op=='getStatisticsData'){
    $request=array();
    $statistics=pdo_get('cqkundian_farm_statistics',array('uniacid'=>$uniacid,'date'=>date("Ymd",time())));
    $request['statistics']=$statistics;
    $user_total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('cqkundian_farm_user')." WHERE uniacid=".$uniacid);
    $request['total_user']=$user_total;
    echo json_encode($request);die;
}

if($op=='order_list'){
    $type=$_GPC['type'];
    if(!empty($_GPC['page'])){
        $page=$_GPC['page']+1;
    }else{
        $page=1;
    }
    $current=$_GPC['current'];
    $cond['uniacid']=$uniacid;
    if($current==1){
        $cond['status']=1;
    }elseif ($current==2){
        $cond['status']=2;
    }elseif ($current==3){
        $cond['status']=3;
    }elseif ($current==4){
        $cond['status']=5;
    }
    $setData=pdo_get('cqkundian_farm_set',array('ikey'=>'expire_order_time','uniacid'=>$uniacid));
    if($type==1) {
        $orderData = pdo_getall('cqkundian_farm_shop_order', $cond, '', '', 'create_time desc', array($page, 10));
        for ($i = 0; $i < count($orderData); $i++) {
            //判断订单支付时间是否过期
            if(!empty($setData) && $setData['value']!=0&& $setData['value']>0) {
                if($orderData[$i]['status']==0){
                    if($orderData[$i]['create_time']+$setData['value'] <= time()){
                        pdo_update('cqkundian_farm_shop_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$orderData[$i]['id']));
                    }
                }
            }
            $user = pdo_get('cqkundian_farm_user', array('uid' => $orderData[$i]['uid'], 'uniacid' => $uniacid));
            $orderData[$i]['avatar'] = $user['avatarurl'];
            $orderData[$i]['nickname'] = $user['nickname'];

            //查询商品详细信息
            $orderDetail = pdo_getall('cqkundian_farm_shop_order_detail', array('uniacid' => $uniacid, 'order_id' => $orderData[$i]['id']));
            for ($j = 0; $j < count($orderDetail); $j++) {
                if ($orderDetail[$j]['spec_id'] != 0) {  //存在规格
                    if($orderData[$i]['order_type']==0) {
                        $specVal = pdo_get('cqkundian_farm_goods_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                        $spec_id = explode(",", $specVal['sku_name']);
                        $spec_val = pdo_getall('cqkundian_farm_spec_value', array('id in' => $spec_id));
                        $specItem = pdo_getall('cqkundian_farm_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                        for ($n = 0; $n < count($specItem); $n++) {
                            for ($m = 0; $m < count($spec_val); $m++) {
                                if ($specItem[$n]['id'] == $spec_val[$m]['spec_id']) {
                                    $specItem[$n]['spec_value'] = $spec_val[$m]['spec_value'];
                                }
                            }
                        }
                        $orderDetail[$j]['specItem'] = $specItem;
                    }elseif ($orderData[$i]['order_type']==3){  //家禽宰杀寄送
                        $specVal=pdo_get('cqkundian_farm_animal_sku',array('aid'=>$orderDetail[$j]['goods_id'],'uniacid'=>$uniacid));
                        $spec_id=explode(",", $specVal['sku_name']);
                        $spec_val=pdo_getall('cqkundian_farm_animal_spec_value',array('id in'=>$spec_id));
                        $specItem=pdo_getall('cqkundian_farm_animal_spec',array('aid'=>$orderDetail[$j]['goods_id'],'uniacid'=>$uniacid));
                        for($n=0;$n<count($specItem);$n++){
                            for($m=0;$m<count($spec_val);$m++){
                                if($specItem[$n]['id']==$spec_val[$m]['spec_id']){
                                    $specItem[$n]['spec_value']=$spec_val[$m]['spec_value'];
                                }
                            }
                        }
                        $orderDetail[$j]['specItem']=$specItem;
                    }elseif ($orderData[$i]['order_type']==4){   //种植摘取订单
                        $sendData=pdo_get('cqkundian_farm_send',array('id'=>$orderDetail[$j]['goods_id']));
                        $orderDetail[$j]['goods_name']=$sendData['send_name'];
                    }
                }
            }
            $orderData[$i]['orderDetail'] = $orderDetail;
        }
    }elseif($type==2){
        $orderData=pdo_getall('cqkundian_farm_group_order',$cond,'','','create_time desc',array($page,10));
        for($n=0;$n<count($orderData);$n++){
            if(!empty($setData) && $setData['value']!=0&& $setData['value']>0) {
                if($orderData[$n]['status']==0){
                    if($orderData[$n]['create_time']+$setData['value'] <= time()){
                        pdo_update('cqkundian_farm_group_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$orderData[$n]['id']));
                    }
                }
            }

            $user = pdo_get('cqkundian_farm_user', array('uid' => $orderData[$n]['uid'], 'uniacid' => $uniacid));
            $orderData[$n]['avatar'] = $user['avatarurl'];
            $orderData[$n]['nickname'] = $user['nickname'];
            $orderDetail=pdo_getall('cqkundian_farm_group_order_detail',array('uniacid'=>$uniacid,'order_id'=>$orderData[$n]['id']));
            for ($i = 0; $i < count($orderDetail); $i++) {
                $specVal = pdo_get('cqkundian_farm_group_goods_spec', array('goods_id' => $orderDetail[$i]['goods_id'], 'uniacid' => $uniacid));
                $spec_id = explode(",", $specVal['sku_name']);
                $spec_val = pdo_getall('cqkundian_farm_group_spec_value', array('id in' => $spec_id));
                $specItem = pdo_getall('cqkundian_farm_group_spec', array('goods_id' => $orderDetail[$i]['goods_id'], 'uniacid' => $uniacid));
                for ($j = 0; $j < count($specItem); $j++) {
                    for ($m = 0; $m < count($spec_val); $m++) {
                        if ($specItem[$j]['id'] == $spec_val[$m]['spec_id']) {
                            $specItem[$j]['spec_value'] = $spec_val[$m]['spec_value'];
                        }
                    }
                }
                $orderDetail[$i]['specItem'] = $specItem;
            }
            $orderData[$n]['orderDetail']=$orderDetail;
        }
    }elseif ($type==3){
        $orderData=pdo_getall('cqkundian_farm_integral_order',$cond,'','','create_time desc',array($page,10));
        for ($i = 0; $i < count($orderData); $i++) {
            if(!empty($setData) && $setData['value']!=0&& $setData['value']>0) {
                if($orderData[$i]['status']==0){
                    if($orderData[$i]['create_time']+$setData['value'] <= time()){
                        pdo_update('cqkundian_farm_group_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$orderData[$i]['id']));
                    }
                }
            }

            $user = pdo_get('cqkundian_farm_user', array('uid' => $orderData[$i]['uid'], 'uniacid' => $uniacid));
            $orderData[$i]['avatar'] = $user['avatarurl'];
            $orderData[$i]['nickname'] = $user['nickname'];
            $orderDetail=pdo_getall('cqkundian_farm_integral_order_detail',array('order_id'=>$orderData[$i]['id'],'uniacid'=>$uniacid));
            for ($j = 0; $j < count($orderDetail); $j++) {
                if ($orderDetail[$j]['spec_id'] != 0) {  //存在规格
                    $specVal = pdo_get('cqkundian_farm_integral_goods_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                    $spec_id = explode(",", $specVal['sku_name']);
                    $spec_val = pdo_getall('cqkundian_farm_integral_spec_value', array('id in' => $spec_id));
                    $specItem = pdo_getall('cqkundian_farm_integral_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                    for ($n = 0; $n < count($specItem); $n++) {
                        for ($m = 0; $m < count($spec_val); $m++) {
                            if ($specItem[$n]['id'] == $spec_val[$m]['spec_id']) {
                                $specItem[$n]['spec_value'] = $spec_val[$m]['spec_value'];
                            }
                        }
                    }
                    $orderDetail[$j]['specItem'] = $specItem;
                }
            }
            $orderData[$i]['orderDetail'] = $orderDetail;
        }
    }
    $request['orderData']=$orderData;
    echo json_encode($request);die;
}

//确认发货
if($op=='sureSend'){
    $type=$_GPC['type'];
    $send_number = $_GPC['send_number'];
    $express_company = $_GPC['express_company'];
    $orderid = $_GPC['orderid'];
    $update = array(
        'express_company' => $express_company,
        'send_number' => $send_number,
        'status' => 2,
    );
    if($type==1) {
        $res = pdo_update('cqkundian_farm_shop_order', $update, array('uniacid' => $uniacid, 'id' => $orderid));
    }elseif ($type==2){
        $res=pdo_update('cqkundian_farm_group_order',$update,array('uniacid'=>$uniacid,'id'=>$orderid));
    }elseif ($type==3){
        $res=pdo_update('cqkundian_farm_integral_order',$update,array('uniacid'=>$uniacid,'id'=>$orderid));
    }
    echo $res ? json_encode(array('code'=>1)):json_encode(array('code'=>2));die;
}

//取消订单
if($op=='cancelOrder'){
    $orderid=$_GPC['orderid'];
    $orderData=pdo_get('cqkundian_farm_shop_order',array('uniacid'=>$uniacid,'id'=>$orderid));
    if($orderData['status']==0){
        $res=pdo_update('cqkundian_farm_shop_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$orderid));
        echo $res? json_encode(array('code'=>1,'msg'=>'取消成功')):json_encode(array('code'=>2,'msg'=>'取消失败'));die;
    }elseif ($orderData['status']==4){
        include 'refund.inc.php';
        $setting = uni_setting($_W['uniacid'], array('payment'));
        $wechat = $setting['payment']['wechat'];
        $refund_setting = $setting['payment']['wechat_refund'];
        $sql = 'SELECT * FROM ' . tablename('account_wxapp') . ' WHERE `acid`=:acid';
        $row = pdo_fetch($sql, array(':acid' => $wechat['account']));
        $refundid=refund_create_order1($orderData['order_number'],'kundian_farm');

        $refundlog = pdo_get('core_refundlog', array('id' => $refundid));
        $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'uniontid' => $refundlog['uniontid']));
        //通过微信api进行退款流程
        $result=reufnd_wechat_build($refundid);
        if($result['return_code']=="SUCCESS"){
            $res1=pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refundid));
            $res2=pdo_update('cqkundian_farm_shop_order', array('status' => '5'), array('id' => $orderid));
            if($res1 && $res2){
               echo json_encode(array('code'=>1,'msg'=>'退款成功'));die;
            }else{
                echo json_decode(array('code'=>2,'msg'=>'退款失败'));die;
            }
        }else{
            echo json_decode(array('code'=>2,'msg'=>'退款失败1'));die;
        }
    }
}

//取消组团订单
if($op=='cancelGroupOrder'){
    $orderid=$_GPC['orderid'];
    $orderData=pdo_get('cqkundian_farm_group_order',array('uniacid'=>$uniacid,'id'=>$orderid));
    if($orderData['status']==0){
        $res=pdo_update('cqkundian_farm_group_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$orderid));
        echo $res? json_encode(array('code'=>1,'msg'=>'取消成功')):json_encode(array('code'=>2,'msg'=>'取消失败'));die;
    }elseif ($orderData['status']==4){
        include 'refund.inc.php';
        $setting = uni_setting($_W['uniacid'], array('payment'));
        $wechat = $setting['payment']['wechat'];
        $refund_setting = $setting['payment']['wechat_refund'];
        $sql = 'SELECT * FROM ' . tablename('account_wxapp') . ' WHERE `acid`=:acid';
        $row = pdo_fetch($sql, array(':acid' => $wechat['account']));
        $refundid=refund_create_order1($orderData['order_number'],'kundian_farm');

        $refundlog = pdo_get('core_refundlog', array('id' => $refundid));
        $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'uniontid' => $refundlog['uniontid']));
        //通过微信api进行退款流程
        $result=reufnd_wechat_build($refundid);
        if($result['return_code']=="SUCCESS"){
            $res1=pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refundid));
            $res2=pdo_update('cqkundian_farm_group_order', array('status' => '5'), array('id' => $orderid));
            updateGroupData($orderid,$uniacid);
            if($res1 && $res2){
                echo json_encode(array('code'=>1,'msg'=>'退款成功'));die;
            }else{
                echo json_decode(array('code'=>2,'msg'=>'退款失败'));die;
            }
        }else{
            echo json_decode(array('code'=>2,'msg'=>'退款失败1'));die;
        }
    }
}

//取消积分商城订单
if($op=='cancelIntrgralOrder'){
    $orderid=$_GPC['orderid'];
    $res=pdo_update('cqkundian_farm_integral_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$orderid));
    echo $res? json_encode(array('code'=>1,'msg'=>'取消成功')):json_encode(array('code'=>2,'msg'=>'取消失败'));die;
}

//查询未完成订单详细信息
if($op=='getOrderDetail'){
    $orderid=$_GPC['orderid'];
    $type=$_GPC['type'];
    if($type==1){ //普通商城订单
        $orderData=pdo_get('cqkundian_farm_shop_order',array('uniacid'=>$uniacid,'id'=>$orderid));
        $orderDetail=pdo_getall('cqkundian_farm_shop_order_detail',array('uniacid'=>$uniacid,'order_id'=>$orderid));
        $orderData['create_time']=date("Y-m-d H:i:s",$orderData['create_time']);
        if($orderData['pay_time']!=0){
            $orderData['pay_time']=date("Y-m-d H:i:s",$orderData['pay_time']);
        }else{
            $orderData['pay_time']='--:--';
        }

        for ($j=0;$j<count($orderDetail);$j++) {
            if($orderDetail[$j]['spec_id']!=0) {
                if($orderData[$i]['order_type']==0) {
                    $specVal = pdo_get('cqkundian_farm_goods_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                    $spec_id = explode(",", $specVal['sku_name']);
                    $spec_val = pdo_getall('cqkundian_farm_spec_value', array('id in' => $spec_id));
                    $specItem = pdo_getall('cqkundian_farm_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                    for ($n = 0; $n < count($specItem); $n++) {
                        for ($m = 0; $m < count($spec_val); $m++) {
                            if ($specItem[$n]['id'] == $spec_val[$m]['spec_id']) {
                                $specItem[$n]['spec_value'] = $spec_val[$m]['spec_value'];
                            }
                        }
                    }
                    $orderDetail[$j]['specItem'] = $specItem;
                }elseif ($orderData[$i]['order_type']==3){  //家禽宰杀寄送
                    $specVal=pdo_get('cqkundian_farm_animal_sku',array('aid'=>$orderDetail[$j]['goods_id'],'uniacid'=>$uniacid));
                    $spec_id=explode(",", $specVal['sku_name']);
                    $spec_val=pdo_getall('cqkundian_farm_animal_spec_value',array('id in'=>$spec_id));
                    $specItem=pdo_getall('cqkundian_farm_animal_spec',array('aid'=>$orderDetail[$j]['goods_id'],'uniacid'=>$uniacid));
                    for($n=0;$n<count($specItem);$n++){
                        for($m=0;$m<count($spec_val);$m++){
                            if($specItem[$n]['id']==$spec_val[$m]['spec_id']){
                                $specItem[$n]['spec_value']=$spec_val[$m]['spec_value'];
                            }
                        }
                    }
                    $orderDetail[$j]['specItem']=$specItem;
                }elseif ($orderData[$i]['order_type']==4){   //种植摘取订单
                    $orderDetail[$j]['goods_name']=$orderData['body'];
                }
            }
        }
        $orderData['orderDetail']=$orderDetail;
    }elseif($type==2){
        $orderData=pdo_get('cqkundian_farm_group_order',array('uniacid'=>$uniacid,'id'=>$orderid));
        $orderDetail=pdo_getall('cqkundian_farm_group_order_detail',array('uniacid'=>$uniacid,'order_id'=>$orderid));
        for ($i = 0; $i < count($orderDetail); $i++) {
            $specVal = pdo_get('cqkundian_farm_group_goods_spec', array('goods_id' => $orderDetail[$i]['goods_id'], 'uniacid' => $uniacid));
            $spec_id = explode(",", $specVal['sku_name']);
            $spec_val = pdo_getall('cqkundian_farm_group_spec_value', array('id in' => $spec_id));
            $specItem = pdo_getall('cqkundian_farm_group_spec', array('goods_id' => $orderDetail[$i]['goods_id'], 'uniacid' => $uniacid));
            for ($j = 0; $j < count($specItem); $j++) {
                for ($m = 0; $m < count($spec_val); $m++) {
                    if ($specItem[$j]['id'] == $spec_val[$m]['spec_id']) {
                        $specItem[$j]['spec_value'] = $spec_val[$m]['spec_value'];
                    }
                }
            }
            $orderDetail[$i]['specItem'] = $specItem;
        }
        $orderData['orderDetail']=$orderDetail;

    }elseif ($type==3){
        $orderData=pdo_get('cqkundian_farm_integral_order',array('uniacid'=>$uniacid,'id'=>$orderid));
        $orderDetail=pdo_getall('cqkundian_farm_integral_order_detail',array('order_id'=>$orderid,'uniacid'=>$uniacid));
        for ($j = 0; $j < count($orderDetail); $j++) {
            if ($orderDetail[$j]['spec_id'] != 0) {  //存在规格
                $specVal = pdo_get('cqkundian_farm_integral_goods_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                $spec_id = explode(",", $specVal['sku_name']);
                $spec_val = pdo_getall('cqkundian_farm_integral_spec_value', array('id in' => $spec_id));
                $specItem = pdo_getall('cqkundian_farm_integral_spec', array('goods_id' => $orderDetail[$j]['goods_id'], 'uniacid' => $uniacid));
                for ($n = 0; $n < count($specItem); $n++) {
                    for ($m = 0; $m < count($spec_val); $m++) {
                        if ($specItem[$n]['id'] == $spec_val[$m]['spec_id']) {
                            $specItem[$n]['spec_value'] = $spec_val[$m]['spec_value'];
                        }
                    }
                }
                $orderDetail[$j]['specItem'] = $specItem;
            }
        }
        $orderData['orderDetail'] = $orderDetail;

    }
    $request['orderData']=$orderData;
    echo json_encode($request);die;
}


//删除订单
if($op=='deleteOrder'){
    $type=$_GPC['type'];
    $orderid=$_GPC['orderid'];
    if($type==1){
        pdo_delete('cqkundian_farm_shop_order_detail',array('uniacid'=>$uniacid,'order_id'=>$orderid));
        $res=pdo_delete('cqkundian_farm_shop_order',array('uniacid'=>$uniacid,'id'=>$orderid));
    }elseif ($type==2){
        pdo_delete('cqkundian_farm_group_order_detail',array('uniacid'=>$uniacid,'order_id'=>$orderid));
        $res=pdo_delete('cqkundian_farm_group_order',array('uniacid'=>$uniacid,'id'=>$orderid));
    }elseif ($type==3){
        pdo_delete('cqkundian_farm_integral_order_detail',array('uniacid'=>$uniacid,'order_id'=>$orderid));
        $res=pdo_delete('cqkundian_farm_integral_order',array('uniacid'=>$uniacid,'id'=>$orderid));
    }
    echo $res  ? json_encode(array('code'=>1,'msg'=>'删除成功')) : json_encode(array('code'=>2,'msg'=>'删除失败'));die;
}



/**
 * 退款后更新组团信息
 * @param  [type] $order_id [description]
 * @param  [type] $uniacid  [description]
 * @return [type]           [description]
 */
function updateGroupData($order_id,$uniacid){
    $orderData=pdo_get('cqkundian_farm_group_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $order_detail=pdo_get('cqkundian_farm_group_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    if($order_detail['spec_id']){
        pdo_update('cqkundian_farm_group_goods_spec',array('count +='=>$order_detail['count']),array('id'=>$order_detail['spec_id'],'uniacid'=>$uniacid));
    }else{
        pdo_update('cqkundian_farm_group_goods',array('count +='=>$order_detail['count']),array('goods_id'=>$order_detail['goods_id'],'uniacid'=>$uniacid));
    }

    //删除组团信息
    pdo_delete('cqkundian_farm_group',array('order_id'=>$order_id,'uid'=>$orderData['uid'],'uniacid'=>$uniacid));
}
